package com.hyz.javaunittest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    EditText et1, et2;
    Button btn;
    TextView tvResult;
    public String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getAuthorInfoFromHttp();
    }


    private void getAuthorInfoFromHttp() {
        MyHttpUtils myHttpUtils = new MyHttpUtils();
        myHttpUtils.getAuthorInfo(new MyHttpUtils.CallBack() {
            @Override
            public void getSuccess(String data) {
                MainActivity.this.data = data;
            }

            @Override
            public void getError(String msg) {

            }
        });
    }

    private void initView() {
        et1 = findViewById(R.id.et_1);
        et2 = findViewById(R.id.et_2);
        btn = findViewById(R.id.btn);
        tvResult = findViewById(R.id.tv_result);
        btn.setOnClickListener(this);
        btn.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (!TextUtils.isEmpty(et1.getText().toString()) && !TextUtils.isEmpty(et2.getText().toString())) {
            int n = sum2String(Integer.parseInt(et1.getText().toString()), Integer.parseInt(et2.getText().toString()));
            tvResult.setText("结果等于：" + n);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (!TextUtils.isEmpty(et1.getText().toString()) && !TextUtils.isEmpty(et2.getText().toString())) {
            int n = muti2String(Integer.parseInt(et1.getText().toString()), Integer.parseInt(et2.getText().toString()));
            tvResult.setText("结果等于：" + n);
        }
        return false;
    }

    public int sum2String(int num1, int num2) {
        int result = num1 + num2;
        return result;
    }

    private int muti2String(int num1, int num2) {
        int result = num1 * num2;
        return result;
    }

}
